﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Customer_ShopingCard : System.Web.UI.Page
{
    static DataTable tbGioHang = new DataTable();
    CustormerMethord Cm = new CustormerMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        if (!IsPostBack)
        {
            load_data();
        }
    }
    private void load_data()
    {

        if (Request.QueryString["ProID"] != null)
        {
            string strID = Request.QueryString["ProID"].ToString();



            int ProductID = Convert.ToInt32(strID);
            DataTable dt = Cm.Shopingview(ProductID);
            String ProductName = dt.Rows[0][1].ToString();
            String Catorogy = dt.Rows[0][2].ToString();

            float Weight = float.Parse(dt.Rows[0][4].ToString());
            float Width = float.Parse(dt.Rows[0][5].ToString());
            float Lenght = float.Parse(dt.Rows[0][6].ToString());
            int Price = int.Parse(dt.Rows[0][7].ToString());


        }

        if (Session["Shopping"] == null)
        {
            Response.Redirect("ShopHollow.aspx");
        }
        tbGioHang = (DataTable)Session["Shopping"];
        string strnumber = tbGioHang.Compute("Sum(Total)", "").ToString();
        lblTotal.Text = strnumber;
        gvGioHang.DataSource = tbGioHang;
        gvGioHang.DataBind();

    }


    protected void btnHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }
    protected void btnDeleteShop_Click(object sender, EventArgs e)
    {
        Session["Shopping"] = null;
        Response.Redirect("Home.aspx");
    }
    protected void btnOrder_Click(object sender, EventArgs e)
    {

        Response.Redirect("OrderOnline.aspx");
    }

    protected void gvGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btDelete")
        {
            int index = int.Parse(e.CommandArgument.ToString());
            try
            {
                DataTable dt = (DataTable)Session["Shopping"];
                dt.Rows.RemoveAt(index);
                Session["Shopping"] = dt;
                if (dt.Rows.Count == 0)
                {
                    Response.Redirect("ShopHollow.aspx");
                }
                Response.Redirect("ShoppingCard.aspx");

            }
            catch (Exception ex)
            {
                //Response.Redirect("ShoppingCard.aspx");
            }
        }


    }

    protected void btnupdate_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = (DataTable)Session["Shopping"];
            foreach (GridViewRow r in gvGioHang.Rows)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (int.Parse(gvGioHang.DataKeys[r.DataItemIndex].Value.ToString()) == int.Parse(dr["ProductID"].ToString()))
                    {
                        TextBox t = (TextBox)r.Cells[3].FindControl("txtQuantity");
                        if (Convert.ToInt32(t.Text) <= 0)
                            dt.Rows.Remove(dr);
                        else
                            dr["Quantity"] = t.Text;
                        break;
                    }
                }
            }
            Session["Shopping"] = dt;
            Response.Redirect("ShoppingCard.aspx");
        }
        catch (Exception)
        {
            Response.Write("<script>alert('Đã Phát Sinh Lỗi, Vui Lòng Kiểm Tra Lại!...')</script>");
        }


    }
}